package com.pqc.tbk.web.admin.money;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pqc.tbk.entity.ConstProp;
import com.pqc.tbk.entity.money.ConfirmMoneyOut;
import com.pqc.tbk.service.admin.money.ConfirmMoneyOutService;
import com.pqc.tbk.utils.DateUtil;
import com.pqc.tbk.utils.JqGridSearchTo;
import com.pqc.tbk.utils.JsonMapper;
import com.pqc.tbk.utils.PageUtil;

@Controller
@RequestMapping("/admin/confirmMoney")
public class ConfirmMoneyOutController {

	private ConfirmMoneyOutService confirmMoneyOutService;

	// 领款状态的sql语句
	private final String TO_APPLY = " and state='"
			+ ConstProp.ConfirmMoneyInout.TO_APPLY + "' ";
	private final String HAVE_APPLY = " and state='"
			+ ConstProp.ConfirmMoneyInout.HAVE_APPLY + "' ";
	private final String HAVE_PAY = "and state='"
			+ ConstProp.ConfirmMoneyInout.HAVE_PAY + "' ";
	private final String NOT_PASS = "and state='"
			+ ConstProp.ConfirmMoneyInout.NOT_PASS + "' ";
	// 日志
	private final Logger logger = Logger
			.getLogger(ConfirmMoneyOutController.class);
	// 分页工具
	private PageUtil<ConfirmMoneyOut> pageUtil;
	{
		pageUtil = new PageUtil<ConfirmMoneyOut>();
	}

	@RequestMapping("/_{view}")
	public String redirectList(@PathVariable("view") final String view) {
		return "/admin/money/" + view;
	}

	/**
	 * jqgrid请求的地址 根据请求的state不同生成不同的sql语句
	 * 
	 * @param _search
	 * @param searchTo
	 * @param state
	 * @return
	 */
	@RequestMapping(value = "/outList", method = RequestMethod.GET)
	@ResponseBody
	public String outList(final boolean _search, final JqGridSearchTo searchTo,
			final String state) {
		String jsonString = "";
		String stateCondtion = "";
		if ("to_apply".equals(state)) {
			stateCondtion = TO_APPLY;
		} else if ("have_apply".equals(state)) {
			stateCondtion = HAVE_APPLY;
		} else if ("have_pay".equals(state)) {
			stateCondtion = HAVE_PAY;
		} else if ("not_pass".equals(state)) {
			stateCondtion = NOT_PASS;
		}
		Page<ConfirmMoneyOut> page = null;
		final Pageable pageable = pageUtil.getPageable(searchTo.getPage(),
				searchTo.getRows());
		if (_search) {
			page = pageUtil.searchPage(searchTo, pageable, stateCondtion,
					confirmMoneyOutService);
		} else {
			page = confirmMoneyOutService.getPage(
					stateCondtion + searchTo.getOrderString(), pageable);
		}
		jsonString = pageToString(page);
		return jsonString;
	}

	@RequestMapping(value = "/confirm", method = RequestMethod.GET)
	@ResponseBody
	public String confirm(final String ids, final String result,
			final String state) {
		final String[] idArr = ids.split(",");
		for (int i = 0; i < idArr.length; i++) {
			final ConfirmMoneyOut out = confirmMoneyOutService.findById(Long
					.parseLong(idArr[i]));
			if ("NOT".equals(result)) { // 不同意
				out.setState(ConstProp.ConfirmMoneyInout.NOT_PASS);
			} else {
				if ("to_apply".equals(state)) {// 同意且当前状态为to_apply
					out.setState(ConstProp.ConfirmMoneyInout.HAVE_APPLY);
				} else if ("have_apply".equals(state)) {// 同意且当前状态为have_apply
					out.setState(ConstProp.ConfirmMoneyInout.HAVE_PAY);
				}
			}
			try {
				confirmMoneyOutService.saveOrUpdate(out);
			}
			catch (final Exception e) {
				if (i == 0) {// 第1个 说明没有一个成功
					return ConstProp.Operation.FAILURE;
				} else { // 大于0的话说明部分成功
					return ConstProp.Operation.PART;
				}
			}
		}
		return ConstProp.Operation.SUCCESS;
	}

	/**
	 * 将page<CustomerInfo>对象转化成json格式的数据
	 * 
	 * @param page
	 * @return
	 */
	public String pageToString(final Page<ConfirmMoneyOut> page) {
		final List<ConfirmMoneyOut> list = page.getContent();
		final Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("total", page.getTotalPages());
		dataMap.put("page", page.getNumber());
		dataMap.put("records", page.getTotalElements());

		final List<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

		for (final ConfirmMoneyOut item : list) {
			final Map<String, Object> row = new HashMap<String, Object>();
			row.put("id", item.getId());
			row.put("uname", item.getUname());
			row.put("money_rest", item.getMoney_rest());
			row.put("money", item.getMoney());
			row.put("state", item.getState());
			row.put("apply_time", DateUtil.formatLongDate(item.getApply_time()));
			row.put("update_time",
					DateUtil.formatLongDate(item.getUpdate_time()));
			rows.add(row);
		}
		dataMap.put("rows", rows);
		// 转json格式
		final JsonMapper jsonMapper = JsonMapper.buildNonDefaultMapper();
		final String json = jsonMapper.toJson(dataMap);
		return json;
	}

	@Autowired
	public void setConfirmMoneyOutService(
			final ConfirmMoneyOutService confirmMoneyOutService) {
		this.confirmMoneyOutService = confirmMoneyOutService;
	}

}
